<template>
	<view class="port-container">
		<view class="topVue">
			<view class="status"></view>
			<view class="navVue">
				<image src="../../static/imgs/back.png" style="width: 36rpx; height: 36rpx;" @click="backFun"></image>
				<view class="nav-title">API接口</view>
				<view style="width: 36rpx; height: 36rpx;"></view>	
			</view>
		</view>
		
		<view class="place"></view>
		
		<view class="main">
			<view class="item">
				<view class="">{{$t('交易所')}}</view>
				<view class="right" @click="selectPlatform">
					<text v-if="platform">{{ platform }}</text>
					<text v-else>{{$t('请选择')}}</text>
					<image src="/static/imgs/right-icon.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="item">
				<view class="">APIKEY</view>
				<view class="right">
					<u--input v-model="form.apiKey" :placeholder="$t('请输入内容')"
						placeholderStyle="{color: #999999;text-align: right;}"></u--input>
				</view>
			</view>
			<view class="item">
				<view class="">SERETKEY</view>
				<view class="right">
					<u--input v-model="form.secretKey" :placeholder="$t('请输入内容')"
						placeholderStyle="{color: #999999;text-align: right;}"></u--input>
				</view>
			</view>
			<view class="item">
				<view class="">PH</view>
				<view class="right">
					<u--input v-if="form.platform != 1" v-model="form.passphrase" :placeholder="$t('请输入内容')"
						placeholderStyle="{color: #999999;text-align: right;}"></u--input>
					<text v-if="form.platform == 1">{{ form.passphrase }}</text>
				</view>
			</view>
		</view>
		<view class="btns">
			<u-button class="close" type="primary" :text="$t('取消')" @click="close"></u-button>
			<u-button type="primary" :text="$t('确定')" @tap="submint"></u-button>
		</view>
		<u-picker :show="show" :columns="list" itemHeight="80" @confirm="confirm" @cancel="cancel"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					platform: undefined,
					apiKey: undefined,
					secretKey: undefined,
					passphrase: undefined,
				},
				platform: "",
				show: false,
				list: [
					["BIAN", "OKEX"]
				],
			};
		},
		onLoad() {
			this.getApiDetail();
		},
		watch: {
			"form.platform": {
				handler(newVal) {
					if (newVal == 1) {
						this.form.passphrase = "NO";
					} else if (newVal == 2) {
						this.form.passphrase = "";
					}
				},
				deep: true,
			},
		},
		methods: {
			getApiDetail() {
				this.$http.post("/front/userApi/apiDetail", {}).then((res) => {
					if (res.code == 0) {
						if (res.obj) {
							console.log(res.obj);
							this.platform = res.obj.platformStr;
							this.form.platform = res.obj.platform;
							this.form.apiKey = res.obj.apiKey;
							this.form.secretKey = res.obj.secretKey;
							this.form.passphrase = res.obj.passphrase;
						}
					}
				});
			},
			selectPlatform() {
				this.show = true;
			},
			cancel() {
				this.show = false;
			},
			confirm(e) {
				if (e) {
					if (e.indexs == 0) {
						this.form.platform = 1;
					} else if (e.indexs == 1) {
						this.form.platform = 2;
					}
					this.platform = e.value[0];
				}
				this.show = false;
			},
			close() {
				this.form = {
					platform: undefined,
					apiKey: undefined,
					secretKey: undefined,
					passphrase: undefined,
				};
				this.platform = "";
			},
			backFun(){
				uni.navigateBack({
					delta: 1
				})
			},
			// 获取用户状态
			getUserBaseInfo() {
			  this.$http
			    .post(`/front/user/baseInfo`)
			    .then((res) => {
			      if (res) {
			        uni.setStorageSync("apiStatus", res.obj.apiStatus);
			      }
			    })
			    .catch((err) => {
			      console.error(err);
			    });
			},
			submint() {
				if (!this.form.platform) {
					return this.$util.toast(this.$t("请选择交易所"));
				}
				if (!this.form.apiKey) {
					return this.$util.toast(this.$t("请填写") + 'APIKEY');
				}
				if (!this.form.secretKey) {
					return this.$util.toast(this.$t("请填写") + 'SERETKEY');
				}
				if (this.form.platform == 2 && !this.form.passphrase) {
					return this.$util.toast(this.$t("请填写") + 'PH');
				}
				this.$http
					.post(
						`/front/userApi/bind?platform=${this.form.platform}&apiKey=${this.form.apiKey}&secretKey=${this.form.secretKey}&passphrase=${this.form.passphrase}`
					)
					.then((res) => {
						if (res.code == 0) {
							this.$util.toast(this.$t("添加成功"));
							this.getUserBaseInfo()
							this.close();
						}

					})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.port-container {
		width: 100%;
		height: 100%;
		background: url("../../static/imgs/bg.png") no-repeat;
		background-size: 100% 100%;
		box-sizing: border-box;
		position: fixed;
		.place {
			height: calc(var(--status-bar-height) + 92rpx);
		}
		
		.topVue {
			width: 100%;
			position: absolute;
			left: 0;
			top: 0;
			z-index: 99;
			.status {
				width: 100%;
				height: var(--status-bar-height);
			}
			.navVue {
				width: 100%;
				height: 92rpx;
				background: linear-gradient(to right, #FFFBFF, #FFE7F4);
				display: flex;
				justify-content: space-between;
				align-items: center;
				box-sizing: border-box;
				padding: 0 30rpx;
				.nav-title {
					width: 420rpx;
					height: 92rpx;
					font-size: 36rpx;
					color: #04094C;
					text-align: center;
					line-height: 92rpx;
				}
			}
		}

		.main {
			width: 690rpx;
			// height: 410rpx;
			margin: 30rpx auto 0;
			background: #FFFFFF;
			box-shadow: 0rpx 4rpx 20rpx 0px rgba(0,0,0,0.05);
			border-radius: 20rpx;

			.item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 40rpx 24rpx 40rpx 30rpx;
				color: #333;
				font-size: $uni-font-size-30;

				.right {
					color: $uni-text-color-grey;

					/deep/ .u-input {
						flex: none;
						padding-right: 0 !important;
						border-color: transparent !important;
						background: transparent;
						text-align: right;
						border-radius: 10px;

						.uni-input-input {
							color: #333;
							text-align: right;
						}
					}

					image {
						width: 12rpx;
						margin-left: 26rpx;
					}
				}
			}
		}

		.btns {
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 100rpx 0;
			font-size: $uni-font-size-30;

			.u-button {
				width: 280rpx;
				height: 80rpx;
				border: none;
				background-color: #D55B94;
				border-radius: $border-radius-20;
			}

			.close {
				background: #E9E7E9;
				color: #999999;
			}
		}
	}
</style>